package com.seeyou.lake.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.seeyou.lake.R
import com.seeyou.lake.base.MyBaseActivity
import com.seeyou.lake.bean.UserBean
import com.seeyou.lake.databinding.ActivityMainBinding
import com.seeyou.lake.ui.adapter.MyFragmentAdapter
import com.seeyou.lake.ui.edt.EditActivityMy
import com.seeyou.lake.util.SPUtils
import com.seeyou.lake.viewmodel.MainViewModel
import com.seeyou.lake.widget.InputDialog
import com.seeyou.lake.widget.LoginOutDialog
import com.zackratos.ultimatebarx.ultimatebarx.navigationBar
import com.zackratos.ultimatebarx.ultimatebarx.statusBar
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView


class MainActivityMy : MyBaseActivity<ActivityMainBinding, MainViewModel>() {

    private lateinit var mTitle: ArrayList<String>
    private var mSecretInputDialog: InputDialog? = null

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MainActivityMy::class.java)
            activity.startActivity(intent)
        }
    }

    override fun myViewModel(): MainViewModel {
        return MainViewModel()
    }

    override fun yourViewBing(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun init() {
        statusBar {
            transparent()
            light = false
            lvlColor = Color.WHITE
        }
        navigationBar {
            transparent()
            light = false
        }
        mTitle = arrayListOf(getString(R.string.open), getString(R.string.secret))
        setSupportActionBar(mViewBinding.toolbar)
        /*        supportActionBar?.setDisplayHomeAsUpEnabled(true)
                supportActionBar?.setDisplayShowTitleEnabled(false)*/
        val userBean = SPUtils.getInstance().getObject(SPUtils.SP_USER) as UserBean
        Glide.with(this).load(Uri.parse(userBean.avatar)).into(mViewBinding.ivHeader)

        // loadImg(userBean.avatar)

        mViewBinding.tvEmail.text = userBean.email
        mViewBinding.tvUsername.text = userBean.username

        mViewBinding.collapsingToolbarLayout.title = getString(R.string.app_title)

        mViewBinding.appBarLayout.addLiftOnScrollListener { p0, verticalOffset ->
            if (verticalOffset <= -mViewBinding.headLayout.height / 2) {
                mViewBinding.collapsingToolbarLayout.title = getString(R.string.app_title)
            } else {
                mViewBinding.collapsingToolbarLayout.title = ""
            }
        }

        val fragments = ArrayList<Fragment>()
        fragments.add(NormalFragment.newInstance())
        fragments.add(SecretFragment.newInstance())
        mViewBinding.mainVp.adapter = MyFragmentAdapter(supportFragmentManager, fragments)

        val commonNavigator = CommonNavigator(this)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return mTitle.size
            }

            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                val colorTransitionPagerTitleView = ColorTransitionPagerTitleView(context)
                colorTransitionPagerTitleView.normalColor = Color.parseColor("#c8e6c9")
                colorTransitionPagerTitleView.selectedColor = Color.WHITE
                colorTransitionPagerTitleView.textSize = 14f
                colorTransitionPagerTitleView.text = mTitle[index]
                colorTransitionPagerTitleView.setOnClickListener {
                    mViewBinding.mainVp.setCurrentItem(
                        index
                    )
                }
                return colorTransitionPagerTitleView
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                indicator.setColors(Color.parseColor("#ffffff"))
                return indicator
            }
        }

        mViewBinding.mainIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(mViewBinding.mainIndicator, mViewBinding.mainVp)
        mViewBinding.ivMainAdd.setOnClickListener {
            EditActivityMy.open(this)
        }


        mSecretInputDialog = InputDialog(this).setNativeButton { finish() }.setPositiveButton {
            mVModel.setSecret(userBean.uid,it)
        }

        mVModel.getSecretStatus(userBean.uid)

        val loginOutDialog = LoginOutDialog(this).setPositiveButton {
            GoogleSignIn.getClient(this,
                GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail()
                    .build()
            ).signOut()
            SPUtils.getInstance().clear()
            finish()
        }

        mViewBinding.ivHeader.setOnClickListener {
            loginOutDialog.show()
        }

        mVModel.secretStatusResponse.observe(this){
            if (it.data ==0){
                mSecretInputDialog?.show()
            }
        }

        mVModel.setSecretResponse.observe(this){
            SPUtils.getInstance().putObject(SPUtils.SP_USER,it.data)
            mVModel.toast("Secret Set Success")
        }

    }

}
